<?php

class Social_Plugin_Service_Facebook extends Social_Plugin_Service_Abstract
{

    /**
     * do init called whenever it's startup.
     */
    protected function _init()
    {
        $this -> _options = Nuo::getTable('core@setting') -> getAll('social.facebook');

        require_once 'facebook/facebook.php';

        $this -> _plugin = new Facebook( array(
            'app_id' => $this -> getOption('key'),
            'secret' => $this -> getOption('secret'),
        ));
    }

    public function getAuthUrl($params = array(), $callback = null)
    {
        $params['perms'] = 'email';
        $callback = Nuo::url('social/connect/facebook/callback');
        $cancelUrl = Nuo::url('social/connect/facebook/cancel');

        return $this -> _plugin -> getLoginUrl($callback, $cancelUrl, $params);
    }

    public function getLogoutUrl()
    {
        $next = Nuo::url('');
        return $this -> _plugin -> getLogoutUrl($next);
    }

    public function isConnected()
    {

    }

    public function isLogged()
    {

    }

    /**
     * process do connect by session return data
     * @param Request $request
     * @return void
     */
    public function doConnect($request = null)
    {
        $session = $request -> getParam('session');

        $session = json_decode($session, 1);

        $this -> _plugin -> setSession($session);

        $me = $this -> _plugin -> api('/me');

        // test that session is work fine.

        $_SESSION['facebook'] = $session;

        // call hook after process
        Nuo::fireEvent('afterFacebookConnect');
    }

    /**
     * process do logout
     * @param Request $request
     * @return void
     */
    public function doLogout($request = null)
    {
        unset($_SESSION['facebook']);
        Nuo::fireEvent('afterFacebookDisconnect');
    }

}
